package com.gzwanchuang.xhgj.ui.page.mine;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.bean.HeXiaoBean;
import com.gzwanchuang.xhgj.bridge.data.repository.Apis;
import com.gzwanchuang.xhgj.bridge.request.MineRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.HeZhangDetaiViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentHeZhangDetailBinding;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.xhgj.proto.Mergedintoaccount;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.List;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class HeZhangDetailFragment extends BaseFragment {


    private FragmentHeZhangDetailBinding mBinding;
    private HeZhangDetaiViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(HeZhangDetaiViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_he_zhang_detail, container, false);
        mBinding = FragmentHeZhangDetailBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        Mergedintoaccount.detail_query.Builder builders = Mergedintoaccount.detail_query.newBuilder();
        builders.setCashNo(getArguments().getString("cash_no"));
        NetworkManager.INSTANCE.post(Apis.detail_query, builders.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mergedintoaccount.detail_query detail_query = Mergedintoaccount.detail_query.parseFrom(response);
                Log.e("detail_query", detail_query.toString());
                List<Mergedintoaccount.order> orders = detail_query.getOrderListList();
                mViewModel.order_no.set(detail_query.getOrderNo());
                for (Mergedintoaccount.order item : orders) {
                    HeXiaoBean bean = new HeXiaoBean();
                    bean.setAmount(item.getAmount());
                    bean.setFlag(item.getFlag());
                    bean.setOrder_no(item.getOrderNo());
                    bean.setPay_type(item.getPayType());
                    bean.setPay_type_desc(item.getPayTypeDesc());
                    bean.setStatus(item.getStatus());
                    bean.setTime(item.getTime());
                    bean.setSelect(false);
                    mViewModel.data.add(bean);
                }
                mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
                if (detail_query.getStatus().equals("FAIL")) mBinding.btnChuZhang.setVisibility(View.VISIBLE);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showShortToast(errorMessage);
            }
        });


    }

    @Override
    public void loadInitData() {
        super.loadInitData();
    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void itemOnClick(int position) {
            HeXiaoBean bean = mViewModel.data.get(position);
        }

        public void loadMoreListener() {
            mViewModel.page = mViewModel.adapter.get().getData().size() / 10 + 1;
            mRequestViewModel.system_log(mViewModel.page);
        }

        public void zhang() {
            Mergedintoaccount.order_again.Builder builders =  Mergedintoaccount.order_again.newBuilder();
            builders.setFailNo(mViewModel.order_no.get());
            NetworkManager.INSTANCE.post(Apis.order_again, builders.build().toByteArray(), new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) {

                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                   showShortToast(errorMessage);
                   nav().navigateUp();
                }
            });
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
    }
}
